var movie = require('../models/movie')
var Category = require('../models/category')

exports.index = function(req,res){

	Category
		.find({})
		.populate({path: 'movies',options:{limit:5}})
		.exec(function(err,categories){
			if(err){
				console.log(err)
			}
			console.log(categories)
			res.render('index',{
				title: 'iMovie 首页',
				categories: categories
			})
		})
}

/*exports.search = function(req,res){
	var catId = req.query.cat
	var page = req.query.p
	var index = page * 1


	Category
		.find({_id: catId})
		.populate({
			path: 'movies',
			options:{limit:1, skip: index}
		})
		.exec(function(err,categories){
			if(err){
				console.log(err)
			}
			
			var category = categories[0] || {}

			res.render('results',{
				title: 'iMovic 结果列表页',
				keyword: category.name,
				category: category
			})
		})
}*/

exports.search = function(req,res){
	//var page = req.query.p
	
	Category.paginate({}, { page: 1 , limit: 1 }, function(err, result) {
		if(err){
			console.log(err)
		}
  		console.log(result)
  		res.send(console.log(result))
	});

}